package com.orangelife.service.propFeeAccountFrozen.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeAccountFrozen.IPropFeeAccountFrozenService;
import com.orangelife.service.propFeeAccountFrozen.model.PropFeeAccountFrozenDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeAccountFrozen类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-11-24 14:12:26
 * ==============================================================
 */
@Service("propFeeAccountFrozenService")
public class PropFeeAccountFrozenServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeAccountFrozenService {
	private static Logger logger = Logger
			.getLogger(PropFeeAccountFrozenServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeAccountFrozenServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeAccountFrozen(
			PropFeeAccountFrozenDto propFeeAccountFrozen)
			throws AppBizException {

		Assert.notNull(propFeeAccountFrozen);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeAccountFrozen.getStatus())) {
			if (propFeeAccountFrozen.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"状态(1冻结，2解冻扣减，3解冻转账)长度超出");
			}
		}

		propFeeAccountFrozen.setFrozenID(IdUtil.getuuId());
		propFeeAccountFrozen.setItemStat(TypeConst.FLAG_ON);
		propFeeAccountFrozen.setTscreate(new Date());
		propFeeAccountFrozen.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeAccountFrozenMapper.insertSelective",
				propFeeAccountFrozen);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeAccountFrozen(
			PropFeeAccountFrozenDto propFeeAccountFrozen)
			throws AppBizException {

		Assert.notNull(propFeeAccountFrozen);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeAccountFrozen.getFrozenID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeAccountFrozenDto oldPropFeeAccountFrozen = this
				.findPropFeeAccountFrozenByID(propFeeAccountFrozen
						.getFrozenID());

		if (!StringUtils.isBlank(propFeeAccountFrozen.getStatus())) {
			if (propFeeAccountFrozen.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"状态(1冻结，2解冻扣减，3解冻转账)长度超出");
			}
		}

		if (oldPropFeeAccountFrozen == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeAccountFrozen.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeAccountFrozenMapper.updateByPrimaryKeySelective",
				propFeeAccountFrozen);
	}

	@Override
	public PropFeeAccountFrozenDto findPropFeeAccountFrozenByID(
			String propFeeAccountFrozenID) throws AppBizException {
		if (StringUtils.isBlank(propFeeAccountFrozenID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeAccountFrozenDto oldPropFeeAccountFrozen = this.getSqlSession()
				.selectOne(
						"PropFeeAccountFrozenSearchMapper.selectByPrimaryKey",
						propFeeAccountFrozenID);
		if (oldPropFeeAccountFrozen == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeAccountFrozen.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeAccountFrozen;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeAccountFrozenByLogic(String propFeeAccountFrozenID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeAccountFrozenID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeAccountFrozenDto oldPropFeeAccountFrozen = this
				.findPropFeeAccountFrozenByID(propFeeAccountFrozenID);

		oldPropFeeAccountFrozen.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeAccountFrozen.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeAccountFrozenMapper.updateByPrimaryKeySelective",
				oldPropFeeAccountFrozen);
	}

	@Override
	
	public List<Map<String, Object>> findPropFeeAccountFrozenByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeAccountFrozenSearchMapper.findAllPropFeeAccountFrozen",
				map);
		return list;
	}

	@Override
	public List<PropFeeAccountFrozenDto> getPropFeeAccountFrozenByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeAccountFrozenDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeAccountFrozenSearchMapper.getAllPropFeeAccountFrozenDto",
						map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeAccountFrozenByRelevantID(
			String frozenID, String linkID, String accountID, String status)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("frozenID", frozenID);
		map.put("linkID", linkID);
		map.put("accountID", accountID);
		map.put("status", status);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeAccountFrozenSearchMapper.selectPropFeeAccountFrozenDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeAccountFrozenDto> findPropFeeAccountFrozenByRelevantID(
			String frozenID, String linkID, String accountID, String status)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("frozenID", frozenID);
		map.put("linkID", linkID);
		map.put("accountID", accountID);
		map.put("status", status);

		List<PropFeeAccountFrozenDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeAccountFrozenSearchMapper.findPropFeeAccountFrozenDtoByRelevantID",
						map);
		return list;
	}

	@Override
	
	public List<PropFeeAccountFrozenDto> bauthfindPropFeeAccountFrozen(
			List<String> propFeeAccountFrozenIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeAccountFrozenDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeAccountFrozenSearchMapper.batchFindPropFeeAccountFrozen",
						propFeeAccountFrozenIDs);
		return list;
	}

	@Override
	
	public List<Map<String, Object>> bauthfindPropFeeAccountFrozenByMap(
			Map<String, Object> propFeeAccountFrozen) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeAccountFrozenSearchMapper.batchFindPropFeeAccountFrozenByMap",
						propFeeAccountFrozen);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

}